package com.sena.crawler.run;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sena.crawler.util.JedisUtilNew;
import com.sena.crawler.util.ReadProperties;
import redis.clients.jedis.ShardedJedis;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;

public class RunInsertSenaKeyWord {


    public static void main(String[] args) {
        insertKeyWordData();
    }

    public static void insertKeyWordData() {
        JedisUtilNew jedisUtilNew = new JedisUtilNew(ReadProperties.getRedisUrlValue("url"));
        ShardedJedis client = jedisUtilNew.getInstance();
        Map<String, String> map = client.hgetAll("KEY_WORLD");

        String url = ReadProperties.getJDBCUrlValue("url");
        String sql_user = ReadProperties.getJDBCUrlValue("username");
        String sql_pwd = ReadProperties.getJDBCUrlValue("password");
        HashMap<String, Integer> resourceMap = getResourceWsName();
        HashSet<String> set = new HashSet<String>();
        int count = 0;
        List<String> addList = new ArrayList<String>();
        Connection conn = null;
        Statement stmt = null;
        if (resourceMap.size() > 0) {
            for (Map.Entry<String, String> entry : map.entrySet()) {
                int num = 0;
                JSONObject urlVo = JSON.parseObject(entry.getValue());
                for (Map.Entry<String, Integer> resource : resourceMap.entrySet()) {
                    if (urlVo.getString("url").contains(resource.getKey())) {
                        num += 1;
                        JSONArray KList = JSONArray.parseArray(urlVo.getString("kList"));
                        String keys = "";
                        if (KList != null) {
                            for (int i = 0; i < KList.size(); i++) {
                                keys += KList.get(i) + ";";
                            }
                        }
                        String insertSql = "insert into sena_link_scan_key(res_id,scan_url,scan_time,`keys`) values(" + resource.getValue() + ",'" + urlVo.getString("url") + "','" + getYearMonthDayHourMinuteSecond(urlVo.getLong("date")) + "','" + keys + "');";
                        addList.add(insertSql);
                        count++;

                    }

                }
                if (num == 0) {
                    set.add(urlVo.getString("url"));
                }
            }

            if (addList.size() > 0) {
                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    conn = DriverManager.getConnection(url, sql_user, sql_pwd);
                    stmt = conn.createStatement();
                    conn.setAutoCommit(false);
                    stmt = conn.createStatement();

                    for (int i = 0; i < addList.size(); i++) {
                        stmt.addBatch(addList.get(i).toString());
                    }
                    stmt.executeBatch();
                    conn.commit();
                    System.out.println("批量插入==" + addList.size());

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    addList.clear();
                    if (conn != null) {
                        try {
                            conn.close();
                        } catch (Exception e) {
                        }
                    }
                    if (stmt != null) {
                        try {
                            stmt.close();
                        } catch (Exception e) {
                        }
                    }
                }

            }
            for (String k : set) {
                System.out.println("k==" + k);
            }
        }

    }

    public static HashMap<String, Integer> getResourceWsName() {
        String url = ReadProperties.getJDBCUrlValue("url");
        String sql_user = ReadProperties.getJDBCUrlValue("username");
        String sql_pwd = ReadProperties.getJDBCUrlValue("password");
        String querySql = "select *from sena_resource where Id>3";
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, sql_user, sql_pwd);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(querySql);
            while (rs.next()) {
                URL domain = new URL(rs.getString("ws_url"));
                map.put(domain.getHost(), rs.getInt("Id"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception e) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception e) {
                }
            }
        }
        return map;
    }

    /**
     * 将毫秒转换为年月日时分秒
     *
     * @author GaoHuanjie
     */
    public static String getYearMonthDayHourMinuteSecond(long timeMillis) {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
        calendar.setTimeInMillis(timeMillis);
        int year = calendar.get(Calendar.YEAR);

        int month = calendar.get(Calendar.MONTH) + 1;
        String mToMonth = null;
        if (String.valueOf(month).length() == 1) {
            mToMonth = "0" + month;
        } else {
            mToMonth = String.valueOf(month);
        }

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String dToDay = null;
        if (String.valueOf(day).length() == 1) {
            dToDay = "0" + day;
        } else {
            dToDay = String.valueOf(day);
        }

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hToHour = null;
        if (String.valueOf(hour).length() == 1) {
            hToHour = "0" + hour;
        } else {
            hToHour = String.valueOf(hour);
        }

        int minute = calendar.get(Calendar.MINUTE);
        String mToMinute = null;
        if (String.valueOf(minute).length() == 1) {
            mToMinute = "0" + minute;
        } else {
            mToMinute = String.valueOf(minute);
        }

        int second = calendar.get(Calendar.SECOND);
        String sToSecond = null;
        if (String.valueOf(second).length() == 1) {
            sToSecond = "0" + second;
        } else {
            sToSecond = String.valueOf(second);
        }
        return year + "-" + mToMonth + "-" + dToDay + " " + hToHour + ":" + mToMinute + ":" + sToSecond;
    }
}
